package a51qindian.com.comprehensive.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.li.interfaces.OnItemClickListener;
import com.li.widget.CircleImageView;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.model.StaffInfo;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Li
 * Date: 2017/9/25 0025
 * Description: 选择员工
 */

public class ChooseStaffAdapter extends RecyclerView.Adapter<ChooseStaffAdapter.ChooseStaffHolder> {

    private List<StaffInfo> mStaffInfoList;
    private LayoutInflater mInflater;
    private Context mContext;
    public ChooseStaffAdapter(Context context){
        mContext=context;
        mInflater=LayoutInflater.from(context);
    }
    private OnItemClickListener mItemClickListener;


    @Override
    public ChooseStaffHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=mInflater.inflate(R.layout.item_city_staff,parent,false);
        final ChooseStaffHolder holder=new ChooseStaffHolder(view);
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(v,holder.getLayoutPosition());
                }
            });
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(ChooseStaffHolder holder, int position) {
        StaffInfo staffInfo=mStaffInfoList.get(position);
        holder.mTxtStaffName.setText(staffInfo.getRealname());
        holder.mTxtStaffPhone.setText(staffInfo.getMobile());

        String taskNum=staffInfo.getNumber()+"个任务";
        holder.mTxtStaffNum.setText(taskNum);
        RequestOptions options=new RequestOptions();
        options.placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher);
        Glide.with(mContext).load(staffInfo.getAvatar()).apply(options).into(holder.mImgHeader);
    }

    @Override
    public int getItemCount() {
        return mStaffInfoList==null?0:mStaffInfoList.size();
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public void setStaffInfoList(List<StaffInfo> staffInfoList) {
        mStaffInfoList = staffInfoList;
        notifyDataSetChanged();
    }

    static class ChooseStaffHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.img_header)
        CircleImageView mImgHeader;
        @BindView(R.id.txt_staff_name)
        TextView mTxtStaffName;
        @BindView(R.id.txt_staff_phone)
        TextView mTxtStaffPhone;
        @BindView(R.id.img)
        ImageView mImg;
        @BindView(R.id.txt_staff_num)
        TextView mTxtStaffNum;
        public ChooseStaffHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
